# -*- coding: utf-8 -*-

from django.db import models
from django.contrib.auth.models import User

# Create your models here.
from django.utils.translation import ugettext_lazy as _


class Project(models.Model):
    name = models.CharField('产品名', max_length=255)
    domain = models.CharField('域名', max_length=255)
    description = models.TextField('描述', null=True, blank=True)
    status = models.BooleanField('产品状态', default=True)
    created_user = models.ForeignKey(User)
    created_time = models.DateTimeField(help_text=_('created date'), auto_now_add=True)
    updated_time = models.DateTimeField(help_text=_('updated date'), auto_now=True)

    def __unicode__(self):
        return self.name


class Module(models.Model):
    project = models.ForeignKey(Project)
    name = models.CharField(max_length=255)
    status = models.BooleanField(default=True)
    created_user = models.ForeignKey(User)
    created_time = models.DateTimeField(help_text=_('created date'), auto_now_add=True)
    updated_time = models.DateTimeField(help_text=_('updated date'), auto_now=True)

    def __unicode__(self):
        return self.name


class Api(models.Model):
    name = models.CharField(max_length=255)
    url = models.CharField(max_length=255)
    method = models.CharField(max_length=10)
    headers = models.TextField()
    req_type = models.CharField(max_length=255, null=True)
    res_type = models.CharField(max_length=255, null=True)
    exp = models.TextField('返回示例', null=True, blank=True)

    module = models.ForeignKey(Module)
    status = models.BooleanField(default=True)
    created_user = models.ForeignKey(User)
    created_time = models.DateTimeField(help_text=_('created date'), auto_now_add=True)
    updated_time = models.DateTimeField(help_text=_('updated date'), auto_now=True)
    desc = models.TextField('描述', null=True, blank=True)
    is_login = models.BooleanField(default=True)

    def __unicode__(self):
        return self.name


class Api_parm(models.Model):
    name = models.CharField(max_length=255)
    must_option = models.BooleanField(default=False)
    data_type = models.CharField(max_length=255)
    desc = models.TextField('描述', null=True, blank=True)
    api = models.ForeignKey(Api)

    def __unicode__(self):
        return self.name



class Case(models.Model):
    name = models.CharField(max_length=255)
    api = models.ForeignKey(Api)
    body = models.TextField()
    check_sql = models.TextField()
    expected_status = models.IntegerField()
    expected = models.TextField()
    Actual_status = models.IntegerField('实际状态', null=True, blank=True)
    Actual_content = models.TextField('实际返回内容', null=True, blank=True)
    Result = models.BooleanField()
    run_time = models.DateTimeField(null=True,blank=True)
    elapsed = models.IntegerField('耗时', blank=True, null=True)
    pre_Case = models.CharField('前置case', max_length=255, blank=True, null=True)
    status = models.BooleanField(default=True)
    created_user = models.ForeignKey(User)
    created_time = models.DateTimeField(help_text=_('created date'), auto_now_add=True)
    updated_time = models.DateTimeField(help_text=_('updated date'), auto_now=True)
    desc = models.TextField('描述', null=True, blank=True)

    def __unicode__(self):
        return self.name


class Report(models.Model):
    case = models.ForeignKey(Case)
    content = models.TextField()
    detail = models.TextField()
    error = models.TextField(null=True, blank=True)
    dbresult = models.TextField()
    created_user = models.ForeignKey(User)
    status = models.BooleanField()
    created_time = models.DateTimeField(help_text=_('created date'), auto_now_add=True)

    def __unicode__(self):
        return self.content


class DbConfigure(models.Model):
    project = models.ForeignKey(Project)
    address = models.CharField(max_length=255)
    username = models.CharField(max_length=255)
    password = models.CharField(max_length=255)
    dbname = models.CharField(max_length=255)
    port = models.IntegerField(blank=True, null=True)
    sql = models.FileField(upload_to = './upload/')

    def __unicode__(self):
        return self.address


class VarConfigure(models.Model):
    project = models.ForeignKey(Project)
    var_name = models.CharField(max_length=255)
    var_value = models.CharField(max_length=255)
    desc = models.CharField(max_length=255, null=True, blank=True)

    def __unicode__(self):
        return self.var_name
